package edu.mis.model.goods;

import java.util.List;

import com.jfinal.plugin.activerecord.Record;
import edu.mis.model.Goods;
import edu.mis.model.User;

public class GoodsService {
	

	public boolean register(Goods goods){
		if(goods.findGoodsByName(goods.getName())==null){ 
			return goods.save();
		}else{
			return false;
		}
		
	}
	
	
	
	/**
	 * 根据ID得到商品
	 * @param id
	 * @return
	 */
	public Goods findGoodsById(int id) {
		return Goods.dao.findById(id);
	}
	
	/**
	 * 查询所有商品
	 * @return
	 */
	public List<Goods> findAllGoods(){
		return Goods.dao.findAll();
	}
	/**
	 * 查询所有商品及其类别
	 * @return
	 */
	public List<Record> findAllGoodsWithCategory(){
		return Goods.dao.findAllRecord();
	}
	public boolean updatePrice(int id,int newPrice){
		Goods goods = findGoodsById(id);
		goods.setPrice(newPrice);
		return goods.update();
	}
	/**
	 * 商品分类
	 * @param id
	 * @return
	 */
	public boolean goodsAuthorize(int id, int categoryId) {
		Goods goods = findGoodsById(id);
		goods.setCategoryId(categoryId);
		return goods.update();
	}
}
